//
//  HotTripViewController.m
//  littleLight's Project-A
//
//  Created by dllo on 16/5/28.
//  Copyright © 2016年 light. All rights reserved.
//

#import "HotTripViewController.h"
#import "DaysModel.h"
#import "PictureTableViewCell.h"
@interface HotTripViewController ()< UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *myArray;

@end

@implementation HotTripViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0 / 255.0 green:192 / 255.0 blue:203 / 255.0 alpha:1];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Cancel2"] imageWithRenderingMode:1] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.title = @"旅行日记";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName: [UIFont systemFontOfSize:18],NSFontAttributeName: [UIFont boldSystemFontOfSize:18]}];
    //创建tableView
    self.myTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableView];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.myTableView registerClass:[PictureTableViewCell class] forCellReuseIdentifier:@"reuse"];
     [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self getDataList];
    
    
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)getDataList
{
    [XGNetworking XGGETWithURL:[NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@/waypoints/",self.myId] parameter:nil success:^(id result) {
        NSArray *daysArray = [result objectForKey:@"days"];
        for (NSDictionary *dic in daysArray) {
            DaysModel *model = [[DaysModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.myArray addObject:model];
         //   NSLog(@"%@",model.waypoints);
        }
       // NSLog(@"%ld",self.myArray.count);
       [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.myTableView reloadData];
        
        
    } failure:^(NSError *error) {
        
        
    } network:^(BOOL networking) {
        
        
    }];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    DaysModel *model = [self.myArray objectAtIndex:indexPath.section];
    PictureModel *picModel = [model.waypoints objectAtIndex:indexPath.row];
    
    cell.model =  picModel;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DaysModel *model = [self.myArray objectAtIndex:indexPath.section];
    PictureModel *picModel= [model.waypoints objectAtIndex:indexPath.row];
    //计算文字高度
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
    //这里的宽度一定要和我们显示文字的label的宽度一样;
    CGRect rect = [picModel.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    //rect.size.height 这就是文字的高度
    
    
    return 300 + rect.size.height;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    DaysModel *model = self.myArray[section];
    return model.waypoints.count;
//    return 20;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"%ld",self.myArray.count);
    return self.myArray.count;
    
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    DaysModel *model = [self.myArray objectAtIndex:section];
    NSString *string = [ NSString stringWithFormat:@"第%@天",model.day];
    
    
    
    return string;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
